﻿using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using WebStickyNotes.Contracts;
using WebStickyNotes.Data.Access;
using WebStickyNotes.Helpers;
using WebStickyNotes.Models;
using WebStickyNotes.Resources;

namespace WebStickyNotes.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private readonly string _connectionType;
        private readonly string _connectionString;

        private static readonly List<int> AllowedTaskRoleId = new List<int> { 1, 2 };

        public HomeController()
        {
            _connectionType = ConfigurationManager.AppSettings["ConnectionType"];
            _connectionString = ConfigurationManager.ConnectionStrings["WebStickyNotesData"].ConnectionString;
        }

        [HttpGet]
        [MustSignIn]
        public ActionResult Index()
        {
            using (var supplier = new Supplier(_connectionType, _connectionString))
            {
                var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
                if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
                {
                    return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
                }

                var model = new HomeIndexModel
                                {
                                    Projects = FilteredDataHelper.GetFilteredTaskProjectsForUser(supplier.DataAccess, currentUser)
                                };

                return View(model);
            }
        }

        [HttpGet]
        [MustSignIn]
        public ActionResult ViewProject(int id)
        {
            using (var supplier = new Supplier(_connectionType, _connectionString))
            {
                var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
                if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
                {
                    return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
                }

                var model = new HomeViewProjectModel
                                {
                                    CurrentUser = currentUser
                                };

                var streams = supplier.DataAccess.GetStreamsByProject(id);
                model.TaskSwimLanes = new List<TaskSwimLane>();

                foreach (var taskStream in streams)
                {
                    var taskDetails = new List<TaskDetail>();
                    int taskCount;

                    // Only show the tasks if the stream is set to show tasks
                    if (taskStream.ShowTasks)
                    {
                        taskDetails = supplier.DataAccess.GetTasksByStream(taskStream.Id);
                        taskCount = taskDetails.Count;
                    }
                    else
                    {
                        taskCount = supplier.DataAccess.GetTasksByStream(taskStream.Id).Count;
                    }

                    model.TaskSwimLanes.Add(new TaskSwimLane
                                                {
                                                    TaskStream = taskStream,
                                                    TaskDetails = FilteredDataHelper.GetExtendedTaskDetails(supplier.DataAccess, taskDetails),
                                                    TaskCount = taskCount
                                                });

                }

                return View(model);
            }
        }

        [HttpGet]
        [MustSignIn]
        public ActionResult AccessDenied(string address)
        {
            using (var supplier = new Supplier(_connectionType, _connectionString))
            {
                var model = new AccessDeniedModel
                                {
                                    CurrentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId()),
                                    Message = new Message
                                                  {
                                                      Class = "error",
                                                      Text = string.Format(Labels.AccessDenied, address)
                                                  }
                                };

                // Audit
                AuditHelper.AuditAction(supplier.DataAccess, "Access Denied", model.CurrentUser.Id);

                return View(model);
            }
        }
    }
}